package thread;

//账户
class Account{
	
	int money;  //余额
	Object key=new Object();
 	
	public Account(int mm) {
		 this.money =mm;
	}

	//取钱
	public  void draw(int money) {
		synchronized (key) {
			if (this.money < money) {
				System.out.println("\n余额不足");
			}else {
				//模拟办理时间 1秒钟
				try {
					int rand= (int)(Math.random()*1000);
					Thread.sleep(rand);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				this.money = this.money-money;
				System.out.printf("\n恭喜你，取出了%d元",money);
				System.out.println("余额="+this.money);
			}
		}
		
	}
}

class Person extends Thread {
	
	Account zh; //你的账户
	public Person(Account zh,String name) {
		this.setName(name);
		this.zh=zh;
	}
	
	public void run() {
		String name=this.getName();
		System.out.println(name + "正准备取钱");
		zh.draw(600);
	}
}

/**
 * 多个线程  共享一个资源  ，出现冲突
 * @author fix
 *
 */
public class Test4 {

	public static void main(String[] args) {
		//自己的账户 1000元
		Account zh=new Account(1000);
		Person baba=new Person(zh, "爸爸");
		Person son=new Person(zh, "TEMO");
		baba.start();
		son.start();
	}

}
